﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.Diagnostics;
using System.Windows.Forms;
using Contract;
using System.IO;
using System.Net;
using System.Net.Sockets;

namespace client
{
    public partial class Login : Form
    {
        public static string PCN;
        public static IDataUploader Proxy;
        public NetTcpBinding binding;
        EndpointAddress address;
        ChannelFactory<IDataUploader> channel;
        public Login()
        {
            //Create channel
            InitializeComponent();
            CreateChannel();



        }

        public void CreateChannel()
        {
            binding = new NetTcpBinding();
            binding.TransferMode = TransferMode.Streamed;
            binding.SendTimeout = TimeSpan.MaxValue;
            binding.MaxReceivedMessageSize = int.MaxValue;
            //   MessageBox.Show(Dns.GetHostAddresses(Environment.MachineName)[2].ToString());
            address = new EndpointAddress("net.tcp://" + Dns.GetHostAddresses(Environment.MachineName)[2].ToString() + ":5000/DataUploader");
            channel = new ChannelFactory<IDataUploader>(binding, address);

        }

        private void button1_Click(object sender, EventArgs e)
        {
            Proxy = channel.CreateChannel();

            ////////////////////////////////
            MyUser mu = new MyUser();
            mu.PCN = this.textBox1.Text.Trim();
            mu.Password = this.textBox2.Text.Trim();
            /////////////////////////////////
            //try
            //{
            if (mu.PCN == "" || mu.Password == "")
            {
                MessageBox.Show("Fields Can't be empty");
            }
            else
            {
                //Registering client in server

                IPHostEntry host;
                host = Dns.GetHostEntry(Dns.GetHostName());
                foreach (IPAddress ip in host.AddressList)
                {
                    if (ip.AddressFamily == AddressFamily.InterNetwork)
                    {
                        Proxy.sendClientIP(ip, mu.PCN);
                    }
                }
                try
                {
                    if (Proxy.Login(mu) != null)
                    {
                        MainForm f1 = new MainForm(Convert.ToInt32(mu.PCN), mu.Password);
                        f1.Show();
                    }
                    else
                    {
                        MessageBox.Show("fail-remote");
                    }
                }
                  catch 
                {
                    MessageBox.Show("wrong password or username");
                    Login l1 = new Login();
                    l1.Show();
                }

            }

            
        

            channel.Close();
            this.Hide();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Register fr = new Register();
            fr.Show();
        }

 
    }
}

